import {otherRouter, appRouter} from '@/router/router';
import Util from '@/libs/util';
import Vue from 'vue';
import store from '../index';

const app = {
    state: {
        cachePage: [],
        lang: '',
        isFullScreen: false,
        openedSubmenuArr: [],  // 要展开的菜单数组
        menuTheme: 'dark', // 主题
        themeColor: '',
        pageOpenedList: [{
            title: '首页',
            path: '',
            name: 'home_index'
        }],
        currentModuleName: '',
        currentPageName: '',
        currentPath: [
            {
                title: '首页',
                path: '',
                name: 'home_index'
            }
        ],  // 面包屑数组
        moduleMenuList: Util.getModuleMenuList(),
        menuList: [],
        routers: [
            otherRouter,
            ...appRouter
        ],
        tagsList: [...otherRouter.children],
        messageCount: 0
    },
    mutations: {
        setTagsList (state, list) {
            state.tagsList.push(...list);
        },
        updateMenulist (state, currentModuleName) {
            const loginUser = store.state.user.user;
            let userRoles = [];
            if (loginUser && loginUser.hasOwnProperty('roles_name')) {
                userRoles = loginUser.roles_name;
            }
            console.log('userRoles', userRoles);

            let menuList = [];
            state.currentModuleName = currentModuleName;
            const _appRouter = Util.getModuleAppRoutersByName(currentModuleName);

            _appRouter.forEach((item, index) => {
                if (item.roles !== undefined) {
                    if (Util.anyInOf(userRoles, item.roles)) {
                        if (item.children.length === 1) {
                            if (item.agentLevels != undefined && Util.oneOf('agent', userRoles)) {
                                if (Util.oneOf(loginUser.extension.agent_level, item.agentLevels)) {
                                    menuList.push(item);
                                }
                            } else {
                                menuList.push(item);
                            }
                        } else {
                            let len = menuList.push(item);
                            let childrenArr = [];
                            childrenArr = item.children.filter(child => {
                                if (child.roles !== undefined) {
                                    if (Util.anyInOf(userRoles, child.roles)) {
                                        if (child.agentLevels != undefined && Util.oneOf('agent', userRoles)) {
                                            if (Util.oneOf(loginUser.extension.agent_level, child.agentLevels)) {
                                                return child;
                                            }
                                        } else {
                                            return child;
                                        }
                                    }
                                } else {
                                    return child;
                                }
                            });
                            menuList[len - 1].children = childrenArr;
                        }
                    }
                } else {
                    if (item.children.length === 1) {
                        if (item.agentLevels != undefined && Util.oneOf('agent', userRoles)) {
                            if (Util.oneOf(loginUser.extension.agent_level, item.agentLevels)) {
                                menuList.push(item);
                            }
                        } else {
                            menuList.push(item);
                        }
                    } else {
                        let len = menuList.push(item);
                        let childrenArr = [];
                        childrenArr = item.children.filter(child => {
                            if (child.roles !== undefined) {
                                if (Util.anyInOf(userRoles, child.roles)) {
                                    if (child.agentLevels != undefined && Util.oneOf('agent', userRoles)) {
                                        if (Util.oneOf(loginUser.extension.agent_level, child.agentLevels)) {
                                            return child;
                                        }
                                    } else {
                                        return child;
                                    }
                                }
                            } else {
                                return child;
                            }
                        });
                        let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
                        handledItem.children = childrenArr;
                        menuList.splice(len - 1, 1, handledItem);
                    }
                }
            });

            state.menuList = menuList;
        },
        changeMenuTheme (state, theme) {
            state.menuTheme = theme;
        },
        changeMainTheme (state, mainTheme) {
            state.themeColor = mainTheme;
        },
        addOpenSubmenu (state, name) {
            let hasThisName = false;
            let isEmpty = false;
            if (name.length === 0) {
                isEmpty = true;
            }
            if (state.openedSubmenuArr.indexOf(name) > -1) {
                hasThisName = true;
            }
            if (!hasThisName && !isEmpty) {
                state.openedSubmenuArr.push(name);
            }
        },
        closePage (state, name) {
            state.cachePage.forEach((item, index) => {
                if (item === name) {
                    state.cachePage.splice(index, 1);
                }
            });
        },
        initCachepage (state) {
            if (localStorage.cachePage) {
                state.cachePage = JSON.parse(localStorage.cachePage);
            }
        },
        removeTag (state, name) {
            state.pageOpenedList.map((item, index) => {
                if (item.name === name) {
                    state.pageOpenedList.splice(index, 1);
                }
            });
        },
        pageOpenedList (state, get) {
            let openedPage = state.pageOpenedList[get.index];
            if (get.argu) {
                openedPage.argu = get.argu;
            }
            if (get.query) {
                openedPage.query = get.query;
            }
            state.pageOpenedList.splice(get.index, 1, openedPage);
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        clearAllTags (state) {
            state.pageOpenedList.splice(1);
            state.cachePage.length = 0;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        clearOtherTags (state, vm) {
            let currentName = vm.$route.name;
            let currentIndex = 0;
            state.pageOpenedList.forEach((item, index) => {
                if (item.name === currentName) {
                    currentIndex = index;
                }
            });
            if (currentIndex === 0) {
                state.pageOpenedList.splice(1);
            } else {
                state.pageOpenedList.splice(currentIndex + 1);
                state.pageOpenedList.splice(1, currentIndex - 1);
            }
            let newCachepage = state.cachePage.filter(item => {
                return item === currentName;
            });
            state.cachePage = newCachepage;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        setOpenedList (state) {
            state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [otherRouter.children[0]];
        },
        setCurrentPath (state, pathArr) {
            state.currentPath = pathArr;
        },
        setCurrentPageName (state, name) {
            state.currentPageName = name;
        },
        setAvator (state, path) {
            localStorage.avatorImgPath = path;
        },
        switchLang (state, lang) {
            state.lang = lang;
            Vue.config.lang = lang;
        },
        clearOpenedSubmenu (state) {
            state.openedSubmenuArr.length = 0;
        },
        setMessageCount (state, count) {
            state.messageCount = count;
        },

        setCurrentModuleName (state, name) {
            state.currentModuleName = name;
        }
    }
};

export default app;
